﻿// Netris. A free, open source Tetris(tm) clone
// Copyright (C) 2005  Parez Timothy
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General public License for more details.
// 
// You should have received a copy of the GNU General public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using SdlDotNet;
using SdlDotNet.Sprites;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.IO;

namespace Beaglebits.Games.Netris
{
	internal enum MenuScreenResult
	{
		NewGame,
		About,
		Quit
	}
	
	/// <summary>
	/// Description of MenuScreen.
	/// </summary>
	internal class MenuScreen
	{
		private bool close = false;
		private byte position = 0;
		private string fontPath;
		private Surface menu;
		
		//TODO: create enum
		private MenuScreenResult result = 0;
		
		internal MenuScreenResult Result
		{
			get
			{
				return this.result;
			}
		}
			
		internal MenuScreen()
		{
			
		}
		
		internal void Show()
		{
			//Hook up the event
			Events.KeyboardDown += new KeyboardEventHandler(this.OnKeyboardDown);
			
			//Get the fontPath
			this.fontPath = Path.Combine(Path.Combine(Application.StartupPath,"fonts"),"vera.ttf");
			
			//Create a new surface
			menu = Video.SetVideoModeWindow(270,175);
			Video.WindowCaption = "Netris 2";
			
			//Draw the title
			TextSprite title = new TextSprite("Netris 2",new SdlDotNet.Font(this.fontPath,30),Color.AliceBlue,true);
					
			//Update the screen
			menu.Blit(title,new Point(75,20));

			this.drawMenu();
			
			while (!close)
			{
				while(Events.Poll()){};
			}
			
			//Unhook the event handler
			Events.KeyboardDown -= new KeyboardEventHandler(this.OnKeyboardDown);
			
			//Clean up
			menu.Close();
		}
		
		private void drawMenu()
		{
			//Create the font
			SdlDotNet.Font font = new SdlDotNet.Font(this.fontPath,20);
		
			this.menu.Fill(new Rectangle(70,70,200,200),Color.Black);
			
			TextSprite startSprite = new TextSprite("New Game",font,this.position == 0 ? Color.Orange : Color.White,true,new Point(75,75));
			TextSprite aboutSprite = new TextSprite("About Netris",font,this.position == 1 ? Color.Orange : Color.White,true, new Point(75,100));
			TextSprite quitSprite = new TextSprite("Exit Netris",font,this.position == 2 ? Color.Orange : Color.White,true,new Point(75,125));
						
			SpriteCollection collection = new SpriteCollection();
			collection.Add(startSprite);
			collection.Add(aboutSprite);
			collection.Add(quitSprite);				
			collection.Draw(this.menu);
			
			//Flip the entire surface (this.menu.Update());
			this.menu.Flip();
		}
		
		private void OnKeyboardDown(object sender, KeyboardEventArgs e)
		{
			switch (e.Key)
			{
				case Key.DownArrow:
					if (this.position < 2)
						this.position++;
					else
						this.position = 0;
					this.drawMenu();
					break;
				case Key.UpArrow:
					if (this.position > 0)
						this.position--;
					else
						this.position = 2;
					this.drawMenu();
					break;
				case Key.Escape:
					this.result = MenuScreenResult.Quit;
					this.close = true;
					break;
				case Key.Return:
					switch (this.position)
					{
						case 0:
							this.result = MenuScreenResult.NewGame;
							this.close = true;
							break;
						case 1:
							this.result = MenuScreenResult.About;
							this.close = true;
							break;
						case 2:
							this.result = MenuScreenResult.Quit;
							this.close = true;
							break;						
					}
					break;
			}
		}
	}
}
